﻿using B.S.DataBase.MyProject.Domain;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Implement;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using B.S.DataBase.MyRead.API.Appicant.Command.ApproveCommand;
using B.S.DataBase.MyRead.API.DTOs;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace B.S.DataBase.MyRead.API.Appicant.CommandHandler.ApproveCommandHandler
{
    /// <summary>
    /// /获取审批记录(审批人名称  -- 审批结果)
    /// </summary>
    public class GetLeaveRecordCommandHandler : IRequestHandler<GetLeaveRecordCommand, APIResult<List<UserLeaveRecordDTOs>>>
    {
        private readonly IBaseRepository<LeaveRecordModel> recorRepos;
        private readonly IBaseRepository<User> userRepos;

        public GetLeaveRecordCommandHandler(IBaseRepository<LeaveRecordModel> recorRepos, IBaseRepository<User> userRepos)
        {
            this.recorRepos = recorRepos;
            this.userRepos = userRepos;
        }

        /// <summary>
        /// 获取审批记录(审批人名称  -- 审批结果)
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<List<UserLeaveRecordDTOs>>> Handle(GetLeaveRecordCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<UserLeaveRecordDTOs>> result = new APIResult<List<UserLeaveRecordDTOs>>();
            //var LeaveRecordModelList = await recorRepos.GetAll().Result.Where(x=>x.LeaveId == request.LeaveId && x.DepId == request.DepId && x.LevelRoleId == request.LevelRoleId).ToListAsync();
            //result.data = LeaveRecordModelList.ToList();

            var userlist = await userRepos.GetAll().Result.Where(x => x.LevelRoleId == request.LevelRoleId && x.DepId == request.DepId).ToListAsync();

            List<UserLeaveRecordDTOs> userLeaveRecordDTOs = new List<UserLeaveRecordDTOs>();

            foreach (var item in userlist)
            {
                var recordlists = await recorRepos.GetAll().Result.FirstOrDefaultAsync(x => x.LeaveId == request.LeaveId && x.UserId == item.UserId && x.LevelRoleId == item.LevelRoleId);
                if (recordlists != null)
                {
                    userLeaveRecordDTOs.Add(new UserLeaveRecordDTOs
                    {
                        ApproveNickName = item.NickName,
                        RecordStatus = recordlists.RecordStatus,
                        Reason = recordlists.Reason,
                        LevelRoleId = item.LevelRoleId,
                    });
                }
                else
                {
                    userLeaveRecordDTOs.Add(new UserLeaveRecordDTOs
                    {
                        ApproveNickName = item.NickName,
                        RecordStatus = "",
                        Reason = "",
                        LevelRoleId = item.LevelRoleId,
                    });
                }                                   
            }          

            result.data = userLeaveRecordDTOs.ToList();
            return await Task.FromResult(result);
        }
    }
}
